6 Const Inheri Interf Abst (Unit 1)
6 Const Inheri Interf Abst (Unit 1)
6 Const Inheri Interf Abst (Unit 1)
Constructors, Inheritance,
Inheritance
Syntax:
<class_name>(){}
Example:
class Book{
Book(){
System.out.println("Book is published");}
public static void main(String args[])
{
Book b=new Book(); Output: Book is published
}}
Another Example:
class AddDemo1
{
AddDemo1()
{
int a=10;
int b=5;
int c;
c=a+b;
System.out.println("*****Default Constructor*****");
System.out.println("Total of 10 + 5 = "+c);
}
class abc {
public abc() {
this(5);
System.out.println("Default Constructor");
}
public abc(int x)
{
this(5, 6);
System.out.println("Constructor with one Parameter");
System.out.println("Value of x ==> "+x);
}
public abc(int x, int y)
{
System.out.println("Constructor with two Parameter");
System.out.println("Value of x and y ==> "+x+" "+y);
}
}
class ChainingDemo1
{
public static void main(String as[])
{
abcobj = new abc(); }}
Copy Constructor
• By constructor
• By assigning the values of one object into another
• By clone() method of Object class
Example:
To copy the values of one object into another using constructor:
class Student{
int id; String name;
Student(int i, String n){
id = i;
name = n;
}
Student(Student s){
id = s.id; Output:
name =s.name;
}
void display(){
System.out.println(id+" "+name);}
Constructor Method
compiler provides a default constructor if not specified. not provided by compiler in any case.
name must be same as the class name. may or may not be same as class name.
Constructor Overloading
A technique having class with any number of constructors that differ in parameter lists.
Compiler differentiates these constructors by the number of parameters and their type.
Example: class Student{
int id; String name; int age;
Student(int i,String n){ Output:
id = i; name = n;
} 111 Kiran 0
Student(int i,String n,int a){
id = i; 222 Abhi 25
name = n;
age=a;
}
void display(){
System.out.println(id+" "+name+" "+age);}
public static void main(String args[]){
Student s1 = new Student(111,"Kiran");
Student s2 = new Student(222,"Abhi",25);
s1.display();
s2.display(); } }
INHERITANCE IN JAVA
It is a mechanism in which one object acquires all the properties and behaviors of a parent object.
•It is an important part of OOPs
•Idea behind inheritance: We can create new classes that are built upon existing classes.
•When existing class gets inherited, we can reuse methods and fields of the parent class.
• Moreover, we can add new methods and fields in our current class also.
•Represents the IS-A relationship which is also known as a parent-child relationship.
Usefulness of Inheritance:
•Class: group of objects which have common properties. A blueprint from which objects are created.
•Sub Class/Child Class: class which inherits other class. Also called a derived class or extended class.
•Super Class/Parent Class: class from where a subclass inherits the features. Also called a base class
•Reusability: Able to use the same fields and methods already defined in previous class by a new class
Syntax of Inheritance:
class Subclass-name extends Superclass-name
{
//methods and fields
}
The extends keyword used to make a class that derives from an existing class.
The meaning of "extends" is to increase the functionality.
class which is inherited is called a superclass, and the new class is called subclass.
Example:
In this figure, Programmer is the subclass and Employee is the superclass.
The relationship between the two classes is Programmer IS-A Employee.
It means that Programmer is a type of Employee.
class Employee{
float salary=80000;
}
class Programmer extends Employee{ Output:
int bonus=25000;
public static void main(String args[]){ Programmer salary is:80000.0
Programmer p=new Programmer(); Bonus of programmer is:25000
System.out.println("Programmer salary is:"+p.salary);
System.out.println("Bonus of Programmer is:"+p.bonus);
} }
In this example:
Programmer object can access its own field as well as of Employee class i.e. code
reusability.
Types of Inheritance
1) Single Inheritance: relationship where a class extends the another class.
class B extends A{
int edition=2;
void display() {
System.out.println(" Book Edition= "+ edition);
}}
class C extends A{
String publisher="TMH";
void print() {
System.out.println(" Book Publisher="+publisher);}}
class D{
public static void main(String[] args) {
B b=new B(); b.show(); b.display() ;
C c = new C(); c. show(); c.print();
} }
INTERFACE
An interface is a blueprint of a class. It has static constants and abstract methods.
•It is used to achieve abstraction and multiple inheritance in Java.
•There can be only abstract methods in the java interface not method body.
•Java Interface also represents IS-A relationship.
•It cannot be instantiated just like abstract class.
reasons to use interface:
• to achieve abstraction.
• to support the functionality of multiple inheritance.
Declare an interface: using the interface keyword.
It provides total abstraction; means all the methods in an interface are declared with the empty body, and all the fields
are public, static and final by default.
- A class that implements an interface must implement all methods declared in interface.
• Java compiler adds public & abstract keywords before
Syntax: the interface method.
• Also adds public, static and final keywords before data
interface <interface_name>{ members.
• Thus, Interface fields are public, static and final by
// declare constant fields default, and the methods are public and abstract.
// declare methods that abstract by default.
}
class MyInterface{
public static void main(String args[]){
Bank ob1 = new SBI();
Bank ob2 = new BOB();
System.out.println("Hello");
System.out.println(ob1.printROI());
System.out.println(ob2.printROI()); } }
Multiple inheritance in Java by Interface:
If a class implements multiple interfaces, or an interface extends multiple interfaces, it is known as multiple
inheritance.
Example:
interface Printable{
void print(); }
interface Showable{
void print(); }
Printable and Showable interface have same methods but its implementation is provided
by class MyInterface1, so there is no ambiguity.
Interface Inheritance
A class implements an interface, but one interface extends another interface.
interface Printable{
void print(); }
interface Showable extends Printable{
void show(); }
Output:
Hello Welcome
class MyInterface implements Showable{
public void print()
{ System.out.println("Hello"); }
public void show()
{ System.out.println("Welcome"); }
Syntax:
Method that are declared without any body within an abstract class.
The method body will be defined by its subclass.
Abstract method can never be final and static.
Any class that extends an abstract class must implement all the abstract
methods.
Syntax: abstract return_type function_name ();
About Abstract Class and Method
abstract class A {
abstract void ROI();
public void show() {
System.out.println("this is non-abstract method");
}
}
class B extends A
{
void ROI() {
System.out.println("Calling parent... Rate of Interest!!!!");
}
public static void main(String[] args) {
B b = new B();
b.ROI();
b.show();
} }
Abstraction using Abstract class
Abstraction: An important feature of OOPS.
It means hiding complexity from users and show them only functionality.
Abstract class is used to provide abstraction although it does not provide 100% abstraction because it can also
have concrete method.
abstract class student
{
public abstract void fees();
}
public class accounts extends student {
public void fees() {
System.out.println("Fees is :50000");
// student fees implementation
}
public static void main(String[] args) {
student st= new accounts();
st.fees();
} }
Here by casting instance of accounts type to student reference, we are hiding the complexity
of accounts type under student. Now the student reference can be used to provide the
implementation but it will hide the actual implementation process.
When to use Abstract Methods & Abstract Class?
Abstract methods:
where two or more subclasses are expected to do a similar thing in different ways
through different implementations. These subclasses extend the same Abstract
class and provide different implementations for the abstract methods.
Abstract classes:
used to define generic types of behaviors at the top of an OOPg class hierarchy, and
use its subclasses to provide implementation details of the abstract class.
Abstract class Interface
Abstract class is a class which contain one or Interface is a Java Object containing method
more abstract methods, which has to be declaration but no implementation. The classes
implemented by its sub classes. which implement the Interfaces must provide
the method definition for all the methods.
Abstract class is a Class prefix with an abstract Interface is a pure abstract class which starts
keyword followed by Class definition. with interface keyword.
Abstract class can also contain concrete Whereas, Interface contains all abstract
methods. methods and final variable declarations.
Abstract classes are useful in a situation that Interfaces are useful in a situation that all
Some general methods should be implemented properties should be implemented.
and specialization behavior should be
implemented by child classes.
Thank
You