java classes methods and inheritance
DESCRIPTION
This presentation explains about the Java classes, methods and inheritance concepts. This also explains about the usage of super and this keywords.TRANSCRIPT
Classes, Methods and Inheritance
SCJP / OCJP objectives : 1.1, 1.2, 1.3, 1.4, 5.5
By,Srinivas Reddy.Swww.JAVA9S.comwww.JAVA9S.com
Declaring ClassesSyntax:package com.java9s.ocjp;import com.sun.sample;class Car{ int speed; State void move(){ behaviour //code related to move }}Save the file with Car.java.If there are multiple classes in a file, the file name should
be the name of the class with public access modifier.
www.JAVA9S.comwww.JAVA9S.com
Declaring classes - Rules• The package statement should be the first
statement in a file if the class belongs to a package.
• Import statements comes next to package statement.
• The order packageimportclass should be maintained.
• Comments can come anywhere in the java file.• A java file can have any number of non public
class files
www.JAVA9S.comwww.JAVA9S.com
Declaring classes - Rules
• package and import statements declared in a file apply to all the classes defined in the file.
• A separate .class file will be generated for each class defined in the java file.
• Any name can be given to a file when there is no public class declared in it.
www.JAVA9S.comwww.JAVA9S.com
Creating Objects
Car c = new Car();
DeclarationInstantiation
C is the reference which holds the memory address of the Car object
C
CarSpeed=50
www.JAVA9S.comwww.JAVA9S.com
Creating Objects
Car a = new Car(); Car b = new Car();Car c = new Car();Car d = new Car();
Car e = d;
abcd
e
d.speed =60;System.out.println(e.speed) ;-> 60
www.JAVA9S.comwww.JAVA9S.com
Methods
• Methods are members of a class.• Methods have the behavior of an object.• Methods can be declared with or without
arguments.• Two variants for a method:– Methods that return something– Methods that don’t return anything - void
www.JAVA9S.comwww.JAVA9S.com
Methods – Return typeSyntax:type methodName(arguments){//code that decides the methodsreturn x;}E.g., int addition(int a, int b){
int c = a+b;return c;}
www.JAVA9S.comwww.JAVA9S.com
Methods – void type
Syntax:void methodName(arguments){//Method code.. No need to return anything.}E.g., void saveToFile(String message){//Code related to saving message to file..}
www.JAVA9S.comwww.JAVA9S.com
Method – without argument
Method with No arguments and with a return type:Date getCurrentDate(){return Calender.get(Calender.DAY_OF_MONTH);}
Method with no argument and no return typevoid printCurrentDate(){System.out.println(Calendar.get(Calender.DAY_OF_MONTH));}
www.JAVA9S.comwww.JAVA9S.com
Inheritance
• Inheritance is a way to reuse code from already existing types or objects.
• Inheritance is implemented between two classes using extends keyword.
• When a class extends another class, extending class is called subclass and extended class is super class.
www.JAVA9S.comwww.JAVA9S.com
Inheritance
class Car{ void move(){
System.out.println(“Moves”);}
}class Ford extends Car{ void moveFast(){ System.out.println(“Moves Fast”);
}}
Ford f = new Ford();f.moveFast();f.move();
•Car is super class•Ford is subclass.
www.JAVA9S.comwww.JAVA9S.com
Inheritance
• With inheritance, all the members of super class are available to the subclass objects.
• When a class extends another class, it has an IS-A relation with its super class.
• instanceof keyword can be used to confirm IS-A relationship.
www.JAVA9S.comwww.JAVA9S.com
Right or Wrong??
class Car{ }class Ford extends Car{ }class BMW extends Car{ }
Ford f = new Ford();BMW b = new BMW();Car c = new Car();
f instanceOf Carf instanceOf BMW b instanceOf Fordb instanceOf Carc instanceOf Ford
www.JAVA9S.comwww.JAVA9S.com
super and this keywords
• super is used to access the super class members.
• this is used to access the currently executing objects members.
www.JAVA9S.comwww.JAVA9S.com
super - example
class Car{ int speed;}class Ford extends Car{
int speed; void move(){
System.out.println(“Moving with car speed:”+super.speed)}
}
www.JAVA9S.comwww.JAVA9S.com
this - exampleclass Ford{
int price; setFordPrice(int price){
this.price = price;}
}Ford a = new Ford();a.setFordPrice(3000);‘this’ is not mandatory. But if you have local variables
declared inside a method, to avoid confusion, this can be used to refer to members of the object.
www.JAVA9S.comwww.JAVA9S.com
int price;setFordPrice(int price){this.price = price;}
a
HAS-A relationship
class Student{Pen p = new Pen();
}class Pen{}Student HAS-A pen.
HAS – A relationship helps to reducethe complexity of the classes by the composition of the other classes.
www.JAVA9S.comwww.JAVA9S.com
Thank youFollow me on to get more updates on latest video postsSubscribe on
http://www.youtube.com/user/java9s Twitter : @java9s
facebook: www.facebook.com/java9s
www.JAVA9S.comwww.JAVA9S.com