Professional Documents
Culture Documents
Constructor
A constructor is a special method which is used to create objects of the class.Constructors initialize data
members at the time of creation.The constructor has the same name as the class itself and does not have
return type,not even void.Types of Constructors Default Constructor, Non Parameterized Constructor and
Parameterized Constructor.
No Arguments or Non
ParameterizedConstructor
Parameterized Constructor
package eg01;
public class eg01
{
public static void main(String args[])
{
book mybook=new book();
mybook.bookName="Meat!";
mybook.authorName="Brottman";
mybook.price=40;
mybook.display();
}
}
class book
{
String bookName;
String authorName;
double price;
public book()
{
bookName=B;
authorName=A;
price=0;
}
void display()
{
System.out.println("Book
Name :"+bookName);
System.out.println("Author
Name :"+authorName);
System.out.println("Price
}
}
package eg01;
public class eg01
{
public static void main(String args[])
{
book mybook=new
book(Meat!,Brottman,40);
mybook.display();
}
}
class book
{
String bookName;
String authorName;
double price;
public book(String bName,String
aName,double p)
{
bookName=bName;
authorName=aName;
price=p;
}
void display()
{
System.out.println("Book
Name :"+bookName);
System.out.println("Author
Name :"+authorName);
System.out.println("Price
:"+price);
}
}
If this is
not
present
Java will
provide a
default
:"+price);
Default Constructor
Even if we dont provide any constructors for a Class, we can still call a Nonparamterized Constructor to
create objects of that Class. This is possible Java provides all classes with a Default Constructor which does
nothing. eg public book(){}
Note:When we provide a Constructor for a Class, this Constructor will override the Default Constructor.
Differences between a Constructor and an Ordinary Method or What are the special features of
constructors?
No
Constructor
Ordinary Method
1
2
3
4
Inheritance
Inheritance is a powerful feature of Object Oriented programming which allows new classes to be derived
from existing ones.The child class inherits all the data members and methods of the parent class and may
contain new additionally data members and methods. Inheritance is implemented in Java using the
extends keyword.
Eg class c2 extends c1
where c1 is the parent class and c2 is the child class.
No
2
3
Inheritance Example
Book
Referen
ce
Novel
*Note:All the classes below are placed in the same package eg05;
class book
Super Class
{
String bookName;
String authorName;
double price;
public book(String bName,String aName,double
p)
{
bookName=bName;
authorName=aName;
price=p;
}
void display()
{
System.out.println("Book
Name :"+bookName);
System.out.println("Author
Name :"+authorName);
package eg05;
public class eg05
Main Class
{
public static void main(String args[])
{
novel n1=new novel("Dark Hollow",
"Brian Keane",50,"Horror",7);
n1.displayNovel();
reference r1= new reference("The Werewolf
Book",
"Brad Steiger",120,"Lycanthropy",1999);
r1.displayReference();
}
}
System.out.println("Price
:"+price);
}
}
class novel extends book
{
Sub Class1
String category;
int rating;
public novel(String bName,String
aName,double p,
String cat,int rat)
{
super(bName,aName,p);
Call to Super
category=cat;
Class
rating=rat;
}
public void displayNovel()
{
Call to Super
super.display();
Class Method
System.out.println("Category :"+category);
System.out.println("Rating :"+rating);
}
}
Note: Call to super classs constructor must be the first
2) Reusability of code
Inheritance allows reusability of code ie When we need to create a class similar to an already existing
class, there is no need to start from scratch.We can simply derive a child class from the existing class and
add new data members and methods.
3) Transitive Nature of Inheritance reduces Maintenance
Consider the Class hierarchy shown.Class A is the superclass from which is derived Class B. Class C and
Class D are further derived from Class B. Now if Class A contains a bug, the bug will also be present in the
Child Classes.But when we correct the bug in Class A, automatically the bug is corrected in ALL the child
classes as well.This transitive nature of inheritance reduces efforts required for maintenance.
Class A
Class B
Class C
Class D
extends keyword
The extends keyword is used implement inheritance in java.For example to derive Class B (sub class)
from Class A (super class) the syntax is : classB extends classA{ .}
Types of Inheritance
No Inheritan
What it is?
ce
1
Simple
One sub class inherits from only one base
Inheritan
class.
ce
Example : Class B is the sub class and it
inherits from base class Class A.
2
Multiple
Inheritan
ce*
Hierarchi
al
Inheritan
ce
Example
Class A
Class B
Class A
Class B
Class C
Class A
Class B
Hybrid
Inheritan
ce
Class C
Class A
Class B
Class C
Class D
Multilevel
Inheritan
ce
Class A
Class B
Class C
private
Data members and
methods
are
accessible only from
within its own class.
protected
Data
members
and
methods are accessible
within its own class,to
other classes in the
same package and to
sub classes even if the
sub class is in a
different package.
public
Data
members
and
methods
are
accessible
everywhere the class is
accessible.
//Base Class
class book
{
String bookName; //Hidden data member
}
package eg05b;
public class eg05b
{
public static void main(String args[])
{
novel n=new novel("Ravens of
Doom","Frankenstein");
n.display();
}
} O/P:
Ravens of Doom
Frankenstein
super keyword
The super keyword is used to refer to the super class of the current class.It may be used to refer to a
super class data member or invoke a super class constructor or a super class method.
Invoking a super class method
super.methodname(<list of arguments>)
Invoking a super class constructor
a)The only way to invoke a superclass constructor from a sub class is
super() or super(<list of arguments>)
b)The call to the superclass constructor must be the first statement in the sub class constructor.
final keyword
1)The final keyword specifies that the value of a variable is permanent and cannot be changed later.
Eg final double PI=3.1416;
2)A final method may not be overridden by methods of the child class.
Eg public final void display() { }
3)A final class cannot be extended. ie it cannot have sub classes.
Eg public final class MyClass {...}
Method Overloading or Function Overloading
Method overloading refers to defining many methods in the same class which have the same name but
different number of arguments or different type of arguments.
package eg05d;
public class eg05d
{
public static void main(String args[])
{
String n="John";
MyClass mc=new MyClass();
mc.display();
mc.display(n);
}
}
No
1
2
class MyClass
{
public void display()
//Overloading
{
System.out.println("Hello there");
}
public void display(String name)
//Overloading
{
System.out.println("Hello there
"+name);
}
}
Method Overloading
Method Overloading takes place for methods
of the same class.
Overloading does not block inheritance from
super class.
Method Overriding
Method Overriding takes place for methods of a
super class and a sub class.
Overriding blocks inheritance from the super class.
ie the sub class method replaces the super class
method.
Overridden methods must have the same
signature. ie A super class method can be
overridden only by a child class method with same
name and same parameter list/type of arguments.
During method overriding, the return type of both
methods must be the same.
Object o;
o="Holiday";
o=new
Scanner(System.in);
o=new A();
class A
{
String name;
}
//Note1: Notice that when overloading, the method signatures (number of arguments or type of parameter
is dffierent while when overriding the the signature is same..
Abstract Classes
An abstract class is a superclass that defines a general concept without providing the implementation of
abstract methods.ie the abstract super class will contain the definition of abstract methods but not the
method bodies.The sub classes have to implement the abstract method and provide the method body. The
abstract class is meant to be extended and we are not allowed to create instances of the abstract class.
An abstract class is created using the abstract keyword.
Interfaces
An interface is a classlike construct that defines a protocol of behavior without providing the
implementation of abstract methods.ie the interface will contain the definition of abstract methods but not
the method bodies.The classes which implement the interface have to implement the abstract method and
provide the method body. The interface is meant to be implemented and we are not allowed to create
instances of the interface. An interface is implemented using the implements keyword. An Interface can
only have static final constants and abstract methods.
Abstract Method
Abstract methods are methods defined in Abstract classes or Interfaces with the key word abstract. They
have no method body and sub classes are expected to provide the method body.
Abstract Class Example
Interface Example
package eg08;
package eg09;
public class eg08
public class eg09
{
{
public static void main(String args[])
public static void main(String args[])
{
{
sub s=new sub();
sub s=new sub();
s.display();
s.display();
}
}
}
}
abstract class disp
interface T1
{
{
public abstract void display();
public static final int k=10;
}
public abstract void display();
}
Abstract
Abstract
Abstract Class
An abstract class may contain normal data
members and methods in addition to abstract
methods.
A child class may only extend one abstract
class as multiple inheritance is not allowed in
Java.
An abstract class may have a constructor
Interface
An Interface can only have static final constants and
abstract methods.
A class may implement more than one interface
thus creating the effect of multiple inheritance.
An interface cannot have a constructor.
Class A
Class B
Class A
Class B
A a=new B();
A a=new B();
You need to go through Practical questions from the text book for this chapter.And it will require time and
effort.
10