Object Oriented Programming
Object
- Anything which is present in the real world and physically existing can be termed as an
Object.
Ex: Car,laptop,Tv etc…….
- The Properties of every object is categorized into 2 types:
States
Behaviours
- States are the properties used to store some data.
- Behaviour are the properties to perform some task.
Note:
Every object will have its own properties & behaviours.
Since the states & behaviours belongs to one particular object , If I need to store all these
things we need one dedicated memory location(Container). But we don’t have any predefined
datatype which helps to create such container. Hence we can design our own datatype with
the help of a class, Which can be used as non-primitive datatype.
class:
- class is a blue print of an object.
- It’s a platform to store states and behaviors of an object.
- class has to be declared using class keyword.
- class can also act as datatype.
-It is used to design a user defined non-primitive datatype to store the details of object.
-We need class for execution of program.
-In a class we can create members like methods( to store behaviors of an object) as well as
variable(to store the states of object).
-Every class-name can be a Non-primitive datatype.
-The java language also comes up with lot of built-in classes i.e, String, System, Scanner
etc……
Ex: class Employee
{
// design variables to store states.
// design methods to store behaviors.
}
Note:
*The non-primitive datatype will have default values i.e null.
Members of Class:
Anything which is declared within class block is known as members of the class.
Ex: Methods, Variables, Constructors etc….
A class will have 2 types of members
Data members
Function members
*Data members are those variables which are declared within the body of the class.
*Function members are those methods/functions which are declared within the body of the
class.
The members of the class can be classified into 2 Types..
1.) Static Members of class.
2.) Non-Static Members of class.
Static Members of class:
Any members of the class which is prefixed with static modifier is known as static member of
class.
In java we have Following static members.
i) Static Method
ii) Static Variable
Static Methods:
-A method which is prefixed with static modifier is known as static methods.
-A block which belongs to a static member is known as Static Context.
Static Variable:
-A variable which is prefixed with static modifier / keyword is known as Static Variable.
-It should be declared inside class block.
How to use a static members of one class inside static context of same class ?
Static member present in same class can be accessed just by using memberName.
How to use a static members of one class inside static context of another class ?
Static member present in different class can be accessed by using class name with member
name.
Ex : [Link]
Access Operator(.) :
*) It is a Binary Operator.
*) It is used to access a member present in another class.
Note:
The Class Loader loads all the static properties inside a memory location called as Class Area
or Static Pool.
Static properties are loaded only once, therefore they will have a single copy.
1a.
package com;
/* Accessing static properties within the same class */
public class Student
{
static int age = 20;
public static void study()
{
[Link]("Student is Studying");
}
public static void main(String[] args)
{
[Link]();
[Link]("------------");
[Link](age);
}
}
o/p:
Student is Studying
------------
20
2a.
package com;
class Employee {
public static int id = 101;
public static void work() {
[Link]("Employee is Working");
}
}
2b.
package com;
/* Accessing static properties in another class */
public class Test {
public static void main(String[] args) {
[Link]([Link]);
[Link]();
}
}
o/p:
101
Employee is Working
[Link]-Static Members of class:
-Any member declared inside class block without prefixing static modifier is known as Non-
static member of a class.
-In java we have following Non-static members
i) Non-static Variable.
ii) Non-static Method.
iii) Constructor.
Note:
-The non-static members gets their memory allocated inside object.
-If we need to access any non-static members…. The first thing we must do is create an
object.
What is Object?
-An object is a block of memory created during in runtime inside heap area.
-The object also known as Instance Of A Class.
-The process of creating an object is known as INSTANCIATION.
Syntax to create an object: new ClassName();
new:
-new is a keyword.
-It is a unary operator.
-It is used to create an object along with constructor of the class.
-new will create a block of memory inside heap area and it will return the address of it.
-The type of address generated for the object will always be specific to its class.
How to use a non-static members inside static context of same/different class ?
Non-Static member present in a class can be accessed within a static method only by creating
the object of the class.
1.
/*
Accessing Non-Static Variables inside same class
*/
class Student
{
// NON-STATIC VARIABLES
int age = 20;
String name = "Dinga";
public static void main(String[] args)
{
[Link]("start");
[Link](new Student().age);
[Link](new Student().name);
[Link]("------------------------ -");
[Link]("Age: "+new Student().age);
[Link]("Name: "+new Student().name);
[Link]("------------------------ -");
[Link](new Student().name+" is "+new Student().age+" years old");
[Link]("*****");
[Link]("end");
}
}
o/p
start
*****
20
Dinga
-------------------------
Age: 20
Name: Dinga
-------------------------
Dinga is 20 years old
*****
end
2a.
class Employee
{
int id = 101;
String name = "Tom";
double salary = 123.45;
}
2b.
/*
Accessing Non-Static Variables in another class
*/
class Test
{
public static void main(String[] args)
{
[Link](new Employee().id);
[Link](new Employee().name);
[Link](new Employee().salary);
}
}
o/p:
101
Tom
123.45
When to declare member as static?
-Static members will have only one copy in the memory.
-If the value of a data member in a class is not changing from object other object then those
data members should be declared as static.
-If a function member / method in a class is using only static members of the class then those
function member / method should be declared as static.
When to declare member as non-static?
-Non-Static members will have multiple copy in the memory.
-If the value of a data member in a class is changing from object other object then those data
members should be declared as non-static.
-If a function member / method in a class is using at least one non-static members of the class
then those function member / method should be declared as non-static.
//Draw memory allocation for both Account and MainAccount class
public class Account {
String name="Allen";
long accno=98765432102L;
double balance=0.0;
String ifsc="SBI0067";
String branch="mgraod";
static String bankName="SBI";
public void showDetails(){
[Link](name);
[Link](accno);
[Link](ifsc);
[Link](branch);
}
public static void showBankName(){
[Link](bankName);
}
public void deposit(double amount){
balance=balance+amount;
[Link]("Balance is "+balance);
}
}
public class MainAccount {
public static void main(String[] args) {
[Link]();
new Account().showDetails();
new Account().deposit(500);
new Account().deposit(1000);
}
}
Reference Variable
It is a type of variable which is used to store address of an object.
Within a reference variables we can’t store primitive values.
Within a primitive variables we can’t store store address of an object.
Within one reference variables we can store ONLY ONE OBJECT ADDRESS.
Multiple reference variables can point to same object.
Syntax: ClassName ref-variable;
1.
/*
Accessing Non-Static Variables inside same class
*/
class Student
{
// NON-STATIC VARIABLES
int age = 20;
String name = "Dinga";
public static void main(String[] args)
{
[Link]("start");
[Link]("*****");
Student s = new Student();
// OBJECT CREATION
[Link]([Link]);
[Link]([Link]);
[Link]("------------------------ -");
[Link]("Age: "+[Link]);
[Link]("Name: "+[Link]);
[Link]("------------------------ -");
[Link]([Link]+" is "+[Link]+" years old");
[Link]("*****");
[Link]("end");
}
}
o/p
start
*****
20
Dinga
-------------------------
Age: 20
Name: Dinga
-------------------------
Dinga is 20 years old
*****
end
2a.
class Employee
{
int id = 101;
String name = "Tom";
double salary = 123.45;
}
2b.
/*
Accessing Non-Static Variables in another class
*/
class Test
{
public static void main(String[] args)
{
Employee emp = new Employee();
[Link]([Link]);
[Link]([Link]);
[Link]([Link]);
}
}
o/p:
101Tom
123.45
public class Account {
String name="Allen";
long accno=98765432102L;
double balance=0.0;
String ifsc="SBI0067";
String branch="mgraod";
static String bankName="SBI";
public void showDetails(){
[Link](name);
[Link](accno);
[Link](ifsc);
[Link](branch);
}
public static void showBankName(){
[Link](bankName);
}
public void deposit(double amount){
balance=balance+amount;
[Link]("Balance is "+balance);
}
public void withdraw(double amount){
balance=balance-amount;
[Link]("Balance is "+balance);
}
}
public class MainAccount {
public static void main(String[] args) {
[Link]();
Account a=new Account();
[Link](a);
[Link]();
[Link](500);
[Link](200);
Account a2=new Account();
[Link](1000);
[Link](100);
Account a3=new Account();
//create 5 account object and perform deposit and withdraw operation
}
}
Default Value
- If a variable is declared and not initialized to any value, then the compiler will automatically
initialize to its default value.
- Default values are applicable only for Member Variables (Static and Non-Static Variables).
Default values are as follows
byte, short, int, long ----> 0
float, double ----> 0.0
char ----> '/u0000' (Unicode value) Java does not understand empty white space( )
boolean ----> false
String ----> null
1.
package com;
class DefaultValuesDemo {
int a;
double b;
char c;
boolean d;
String e;
public static void main(String[] args) {
DefaultValuesDemo dvd = new DefaultValuesDemo();
[Link](dvd.a);
[Link](dvd.b);
[Link](dvd.c);
[Link](dvd.d);
[Link](dvd.e);
}
}
o/p:
0
0.0
2.
package com;
class Car {
int cost = 10;
public static void main(String[] args) {
Car c1 = new Car();
Car c2 = new Car();
[Link]([Link]+" "+[Link]);
[Link] = 15;
[Link] = 23;
[Link]([Link]+" "+[Link]);
}
}
o/p:
10 10
15 23
3.
package com;
class Student {
String name;
int marks;
public static void main(String[] args) {
Student s1 = new Student();
Student s2 = new Student();
[Link]([Link]+" "+[Link]);
[Link]([Link]+" "+[Link]);
[Link]("------------------- ");
[Link] = "Tom";
[Link] = 36;
[Link] = "Jerry";
[Link] = 40;
[Link]([Link]+" "+[Link]);
[Link]([Link]+" "+[Link]);
[Link]("------------------- ");
[Link]([Link]+" has scored "+[Link]+" marks in Java");
[Link]([Link]+" has scored "+[Link]+" marks in Java");
}
}
o/p:
null 0
null 0
-------------------
Tom 36
Jerry 40
-------------------
Tom has scored 36 marks in Java
Jerry has scored 40 marks in Java
Member Variable and Local variable
Member variables are those variables which are declared in the class limit/Scope.
2. They can be accessed globally ie. throughout the class.
3. Member variables are categorized into
a. static
b. Non-static
4. Local Variables are those variables which are declared within a specific scope or limit such
as method, constructor, etc.
5. Local variables are accessible within that specific scope.
6. Default Values are applicable only for Member Variables.
this keyword
1. In java, we can have both member and local variable names same, then always the local
variables will dominate the member variables.
2. In order to avoid the dominating part we make use of "this" keyword.
3. this is keyword which is used to point to the current object/instance.
[Link] is a non-static variable which can be used only inside non-static context.
How to differentiate member and local variable having same name under different situations.
If both member variable and local variable have same name then ,if we want to execute
member variable from same method ,priority is always given for local variable.
public class TypesOfVariable {
//member/class variable
static int a;
public static void test(){
//local variable
int b=20;
[Link](a);
[Link](b);
}
public static void main(String[] args) {
[Link](a);//0
// [Link](b);
test();
}
}
--------------------------------------------------------
public class GlobalVariable //Member
{
static int a;
double b;
public static void test(){
[Link](a);
GlobalVariable g1=new GlobalVariable();
[Link](g1.b);
}
public static void main(String[] args) {
[Link](a);
GlobalVariable g=new GlobalVariable();
[Link](g.b);
}
}
-------------------------------------------------
public class LocalVariable {
public static void run(){
int a=20;
[Link](a);
}
public static void main(String[] args) {
run();
}
}
Member static variable and local variable can be differentiated from static method by using
[Link].
public class Demo3 {
static int a=20;
public static void run(){
int a=30;//high priority
[Link](a);//30
//how to differentiate static mem vari & local var with same name?
//Classname
[Link](Demo3.a);//20
}
public static void main(String[] args) {
// [Link](a);//20
run();
}
}
Member non static variable and local variable can be differentiated from static method by
creating object of the class.
public class Demo4 {
int b=10;
public static void m2(){
int b=30;
[Link](b);//30
[Link](new Demo4().b);//10
}
public static void main(String[] args) {
m2();
}
}
Member static variable and local variable can be differentiated from a non static method by
using [Link] or object creation or this keyword.
public class Demo3 {
static int a=20;
public void play(){
int a=10;
[Link](a);//10
[Link](Demo3.a);//20
[Link](new Demo3().a);//20
[Link](this.a);//20
}
public static void main(String[] args) {
// [Link](a);//20
Demo3 d=new Demo3();
[Link]();
}
}
Member non static variable and local variable can be differentiated from non static method by
using this keyword or object creation.
public class Demo4 {
int b=10;
public void m1(){
int b=20;
[Link](b);//20
[Link](new Demo4().b);//10
//this keyword---point to current object
//it has to be used only inside non static context
[Link](this.b);//10
}
public static void main(String[] args) {
Demo4 d=new Demo4();
d.m1();
}
---------------------------------------------------------------
public class Test {
static int age=23;//mem/global variable
public static void m1(){
int age=30;//local variable highest priority
[Link](age);//30
//differentiate static global and local variable having same name
//using [Link]
[Link]([Link]);//23
}
public static void main(String[] args) {
m1();
// [Link](age);//23
}
}
-------------------------------------------------------------
public class Test1 {
double marks=45.6;
public void m2(){
double marks=50.3;
[Link](marks);//50.3
/*diffrentiate non static global and local variable having same name */
[Link](new Test1().marks);//45.6
//this keyword--point to current object
/*rule
has to used only within non static block */
[Link]([Link]);
}
public static void main(String[] args) {
new Test1().m2();
}
}
------------------------------------------------------------
public class Test2 {
static int a=20;
public void m3(){
int a=30;
[Link](a);
[Link](Test2.a);
//not good pratice
[Link](new Test2().a);
[Link](this.a);
}
public static void main(String[] args) {
Test2 t=new Test2();
t.m3();
}
public class MainTest {
public static void main(String[] args) {
[Link](Test2.a);
Test2 t=new Test2();
[Link](t.a);//not good pratice
}
}
Constructor
[Link] are special type of methods which have same name as the class name.
2. Constructor Name and Class Name should always be same.
3. Constructors will not have return type.
4. Constructors will get executed at the time of object creation.
[Link] class must and should have Constructor.
[Link] the programmer do not write any constructor, then compiler will write default constructor
implicitly.
[Link] cannot be declared as static or final.
[Link] you specify return type for the constructor, then it will be considered as a normal method.
[Link] the programmer writes any constructor explicitly, then complier will not write any
constructor implicitly.
10. Constructors are categorized into 2 types:
a. Default Constructor
b. Custom/User-Defined Constructor
syntax: AccessSpecifier ClassName(optional arguments)
{
// Set of Instructions
}
Note:
Constructor is a special non-static members which is used to load all the non-static members
of a class into the object.
Default Constructor
1. If a constructor is not explicitly present in a class, then the compiler will automatically
generate a
constructor and those constructors are called as Default Constructor.
Default constructor neither accepts any arguments nor has any implementation.
Custom/User-Defined Constructor
1. If a constructor is explicitly defined inside a class by the user or the programmer, then we
refer it as custom/user-defined constructor.
2. They are further categorized into 2 types:
i. Non-Parameterized Custom Constructor
ii. Parameterized Custom Constructor
NOTE: WHEN THERE IS DEFAULT CONSTRUCTOR, THEN CUSTOM
CONSTRUCTOR CANNOT BE PRESENT AND VICE VERSA.
Application of Constructor.
Constructors are used to Intialize the data members of the class.
public class Employee2 {
int id;//102
String name;//Priya
double salary;//6.0
static String comapanyName="Abc";
public Employee2(int id,String name,double salary)//a=102,b="Priya",c=6.0
{
// [Link](a);
// [Link](b);
// [Link](c);
[Link]=id;
[Link]=name;
[Link]=salary;
}
public void showDetails(){
[Link](id);
[Link](name);
[Link](salary);
}
public static void showCompanyName(){
[Link](comapanyName);
}
public void updateSalary(double newSalary){
salary=newSalary;
[Link]("new salary is "+salary);
}
public static void main(String[] args) {
showCompanyName();
Employee2 e1=new Employee2(101, "Ben", 6.2);
[Link]();
[Link]("------------------------");
Employee2 e2=new Employee2(102, "Priya", 6.0);
[Link]();
[Link](6.5);
}
Packages:
A java package is group of classes and interfaces which are related to one single module in
the given project.
Package naming convention
package name is always written in all-lowercase.
package names are always written in reverse order of domain
convention : [Link]
Ex: [Link]
Access Specifier
Access specifiers are used to provide security for the classes and its members by controlling
the visibility.
public :
if you declare any entity as public, then it can be accessed by the classes present in same or
different package.
public entities will have highest visibility and lowest security.
protected :
If you declare any entity as protected, then it can be accessed by the classes present in same
package.
protected entity can be accessed by other class present in different package through
inheritance and creating the object of SUBCLASS ONLY.
pkg-level(default) :
if you declare any entity without using any access specifier keyword then it is considered as
pkg-level member(default member).
if you declare any entity as pkg-level(default), then it can be STRCITLY accessed ONLY by
the classes present in SAME package.
private :
if you declare any entity as private, then it can be accesses ONLY by the class within which
they are declared.
private entities will have highest security and lowest visibility.
package [Link];
public class Sample {
//same class access
public static int a=10;
public static void m1() {
[Link]("public m1 method");
}
protected static int b=20;
protected static void m2() {
[Link]("protected m2 method");
}
static int c=30;
static void m3() {
[Link]("default m3 method");
}
private static int d=40;
private static void m4() {
[Link]("private m4 method");
}
public static void main(String [] args) {
//public members
[Link](a);
m1();
//protected members
[Link](b);
m2();
//default members
[Link](c);
m3();
//private members
[Link](d);
m4();
}
}
package [Link];
public class Test {
//different class access
public static void main(String[] args) {
//public members
[Link](Sample.a);
Sample.m1();
//protected members
[Link](Sample.b);
Sample.m2();
//default members
[Link](Sample.c);
Sample.m3();
//private members---cannot access
[Link](Sample.d);
Sample.m4();
}
package [Link];
import [Link];
public class Run {
//different package access
public static void main(String[] args) {
//public members
[Link](Sample.a);
Sample.m1();
//protected members-------can be accessed only with inheritance
[Link](Sample.b);
Sample.m2();
//default members---cannot access
[Link](Sample.c);
Sample.m3();
}
Constructor Overloading
The Process of having multiple constructors in the same class but difference in arguments.
In order to achieve Constructor Overloading we have to either follow 1 of the following rules.
a. There should be a change in the (length)No of Arguments.
b. There should be a change in the Datatype of the Arguments.
c. There should be a change in the Sequence/Order of Datatype.
package [Link];
public class Demo {
public Demo() {
[Link]("zero arg");
}
public Demo(int a) {
[Link]("int arg");
}
public Demo(String a) {
[Link]("String arg");
}
public Demo(int a,double b) {
[Link]("int double arg");
}
public Demo(double a,int b) {
[Link]("double int arg");
}
public static void main(String[] args) {
Demo d=new Demo();
Demo d1=new Demo(5);
Demo d2=new Demo("Hello");
Demo d3=new Demo(2,4.5);
Demo d4=new Demo(4.5,6);
}
}
package [Link];
public class Student {
int id;
String name;
String email;
public Student(int id,String name) {
[Link]=id;
[Link]=name;
}
public Student(int id,String name,String email) {
[Link]=id;
[Link]=name;
[Link]=email;
}
public void printDetails() {
[Link](id);
[Link](name);
[Link](email);
}
public static void main(String[] args) {
Student s1=new Student(1, "allen", "a@[Link]");
[Link]();
[Link]("------------------------------");
Student s2=new Student(2,"ford");
[Link]();
}
}
Method Overloading
In a class having multiple methods with the same name,but difference in arguments is called
as Method Overloading.
In order to achieve method overloading we need to satisfy either 1 of the following 3 rules.
1. There should be a change in the No of Arguments.
2. There should be a change in the Datatype of the Arguments.
3. There should be a change in the order/sequence of the Datatypes.
Note:
1. Both Static and Non-Static methods can be Overloaded.
2. returntype might be same or different.
Advantage:
It is easy to remember One method which may perform similar operation with different
arguments.
package [Link];
public class Calculate {
public static void add(int a,int b)
{
[Link]("1--"+(a+b));
}
public static void add(int a,double b)
{
[Link]("2--"+(a+b));
}
public static void add(double a,double b)
{
[Link]("3--"+(a+b));
}
public static void add(int a,int b,int c)
{
[Link]("4--"+(a+b+c));
}
public static void main(String[] args) {
add(3,4);
add(4,6.7);
add(5.6,7.8);
add(2,3,4);
}
}
Class diagram
A Class diagram is pictorial representation of class.
Class diagrams helps understanding the relations between classes in much better way.
Relationship:
* One object is having some association/connection between another object is known as
relationship.
* Relationship has been classified into 2 types
1. is-a relationship
2. has-a relationship
is-relationship:
the relationship between 2 objects similar to parent and child is known as is-a relationship
* how to achieve is-a relationship
in java is-a relationship is achieved using inheritance.
INHERITANCE
1. Inheritance is a process of one class acquiring the properties of another class.
2. A class which gives or shares the properties are called as Super , Base or Parent Class.
3. A class which acquires or accepts the properties are called as Sub , Derived or Child Class.
4. In java, we achieve inheritance with the help of 'extends' keyword.
5. Inheritance is also referred as "IS-A" Relationship.
[Link] superclass object we can access ONLY properties of superclass
[Link] subclass object we can access properties of both subclass and superclass.
8. In java, Only Variables and methods are inherited where constructors are not INHERITED.
[Link] data members and function members of the class CANNOT be inherited
[Link] subclass object we can access both static and non-static properties of both subclass
and superclass
Types of Inheritance
1. Single Level Inheritance
2. Multi-Level Inheritance
3. Hierarchical Inheritance
4. Multiple Inheritance
5. Hybrid Inheritance
Single Level Inheritance
one Sub class inheriting from one Single Super class is called as Single level Inheritance.
1a
package [Link];
//super class
public class Sample {
int a=10;
public void count() {
[Link]("Sample class count method");
}
static int c=30;
public static void test() {
[Link]("static method of sample class");
}
1b
package [Link];
//sub class
public class Demo extends Sample
{
double b=3.6;
public void display() {
[Link]("Demo class display method");
}
}
1c
package [Link];
public class MainClass {
public static void main(String[] args) {
//create object of super class
Sample s=new Sample();
//super properties
[Link](s.a);
[Link]();
//child properties cannot be accesed
// [Link](s.b);
// [Link]();
[Link]("--------------------------------");
//create object of subclass
Demo d=new Demo();
//super
[Link](d.a);
[Link]();
//subclass
[Link](d.b);
[Link]();
[Link]("============================");
[Link](Sample.c);
[Link]();
[Link]("-----------------");
[Link](Demo.c);
[Link]();
}
}
2a.
package singlelevel;
public class Father
{
int age = 40;
}
2b.
package singlelevel;
public class Son extends Father
{
String name = "Tom";
}
2c.
package singlelevel;
public class Test {
public static void main(String[] args) {
Son s = new Son();
[Link]([Link]);
[Link]([Link]);
}
}
o/p:
40
Tom
Multi-Level Inheritance
Subclass inheriting the properties of superclass and that superclass inheriting the properties
from another superclass is called as Multilevel inheritance.
1a
package [Link];
public class WhatsAppV1 {
public void sendMsg() {
[Link]("message");
}
1b
package [Link];
public class WhatsAppV3 extends WhatsAppV2{
public void sendVideoCall() {
[Link]("video call");
}
}
1c
package [Link];
public class WhatsAppV2 extends WhatsAppV1{
public void sendVoiceMsg() {
[Link]("voice message");
}
}
1d
package [Link];
public class MainClass {
public static void main(String[] args) {
WhatsAppV1 v1=new WhatsAppV1();
[Link]();
WhatsAppV2 v2=new WhatsAppV2();
[Link]();
[Link]();
WhatsAppV3 v3=new WhatsAppV3();
[Link]();
[Link]();
[Link]();
}
}
2a.
package multilevel;
public class University {
String universityName = "VTU";
void conductExams() {
[Link]("VTU is conducting Exams");
}
}
2b.
package multilevel;
public class College extends University {
String collegeName = "Jspiders";
void providePlacements() {
[Link]("Jspiders Provides Placement");
}
}
2c.
package multilevel;
public class Department extends College {
String departmentName = "Computer Science";
void fest() {
[Link]("CS Department had a Fest called as Equinox");
}
}
2d.
package multilevel;
public class Student {
public static void main(String[] args) {
Department d = new Department();
[Link]("University Name: "+[Link]);
[Link]("College Name: "+[Link]);
[Link]("Department Name: "+[Link]);
[Link]("-----------------");
[Link]();
[Link]();
[Link]();
}
}
o/p:
University Name: VTU
College Name: Jspiders
Department Name: Computer Science
-----------------
VTU is conducting Exams
CS Department had a Fest called as Equinox
Jspiders Provides Placement
Generalization
Declaring common methods and variables of all subclasses in one common superclass.
Specialization
Declaring methods and variables specifically for one subclass.
Multiple inheritance
Subclass inheriting the properties from 2 or more superclass is called as Multiple inheritance.
Java don't support Multiple inheritance.
Multiple inhertiance can be achieved with the concept called as interface.
package [Link];
class Account{
//Generalization
String name;
long accno;
double balance;
public void deposit(double amount) {
balance+=amount;
[Link]("Balance after Deposition:"+balance);
}
public void withdaw(double amount) {
balance-=amount;
[Link]("Balance after withdraw:"+balance);
}
public void checkBalance() {
[Link]("Current Balance is:"+balance);
}
}
class Saving extends Account{
//rate of interest--Specialization
double roi=0.005;
public void calculateRoi() {
balance=balance+(balance*roi);
[Link]("Balance after roi:"+balance);
}
}
class Current extends Account{
//minimum balance
double minBalance=5000.0;
public void showMinimumBalance() {
[Link]("Min Balance is:"+minBalance);
}
}
public class HierarchialExample {
public static void main(String[] args) {
Saving s1=new Saving();
[Link]="tom";
[Link]=9874563211254L;
[Link](2000.0);
[Link](300.0);
[Link]();
[Link]();
// [Link]();//cannot access
}
Hybrid Inheritance
It is the combination of Different types inheritance.
CONSTRUCTOR CHAINING
1. The Process of one constructor calling another constructor is called as constructor
chaining.
2. Constructor Chaining can be achieved only in case of constructor overloading.
3. Constructor Chaining in same class can be achieved using this calling statement ie. this().
Note:
1. this() should always be the first executable line within the constructor.
2. Recursive Chaining is not possible, Therefore if there are 'n' constructors we can have a
maximum of 'n-1' calling statements.
package [Link];
public class Demo {
public Demo() {
this(7);
[Link]("zero arg");
}
public Demo(int a) {
this("hello");
[Link]("int arg");
}
public Demo(String a) {
this(2,4.3);
[Link]("String arg");
}
public Demo(int a,double b) {
this(3.4,5);
[Link]("int double arg");
}
public Demo(double a,int b) {
[Link]("double int arg");
}
public static void main(String[] args) {
Demo d=new Demo();
}
}
package [Link];
public class Student {
int id;
String name;
String email;
public Student(int id,String name) {
[Link]=id;
[Link]=name;
}
public Student(int id,String name,String email) {
this(id,name);
[Link]=email;
}
public void printDetails() {
[Link](id);
[Link](name);
[Link](email);
}
public static void main(String[] args) {
Student s1=new Student(1, "allen", "a@[Link]");
[Link]();
[Link]("------------------------------");
Student s2=new Student(2,"ford");
[Link]();
}
}
Global Constructor chaining:
It is the process of sub class constructor calling super class constructor is known as global
constructor chaining.
Constructor chaining can be achieved by super(): super call statement
super(); can be written explicitly by the programmer or implicitly by the compiler at the
compile time.
If the superclass contains only parameterized constructors then the programmer should write
super(); explicitly and pass the required arguments.
super(); must be written ONLY within the constructor body.
super(); should be always written at the first line of constructor body.
Multiple super(); within the same constructor body is NOT allowed.
super() can be used in 2 ways:
i. implicitly
When we create an object of a class, and if that class has a super class, and if that super class
has a nonparameterized constructor, then the sub class constructor will invoke the super class
constructor implicitly.
1a.
package com;
class Father
{
Father()
{
[Link](1);
}
}
1b.
package com;
class Son extends Father
{
Son()
{
// implicitly super();
[Link](2);
}
}
1c.
package com;
public class Test {
public static void main(String[] args) {
Son s = new Son();
}
}
o/p:
1
2
ii. explicitly
When we create an object of a class, and if that class has a super class, and if that super class
has a parameterized constructor, then the sub class constructor should invoke the super class
constructor explicitly, otherwise we get compile time error.
1a.
package com;
class Father
{
Father(int a)
{
[Link](1);
}
}
1b.
package com;
class Son extends Father
{
Son()
{
super(10);
[Link](2);
}
}
1c.
package com;
public class Test {
public static void main(String[] args) {
Son s = new Son();
}
}
o/p:
1
2
2a.
package com;
class Vehicle
{
Vehicle(String brand)
{
[Link]("brand: "+brand);
}
}
2b.
package com;
class Bike extends Vehicle
{
Bike(int cost)
{
super("BMW");
[Link]("cost: "+cost);
}
}
2c.
package com;
public class Solution {
public static void main(String[] args) {
Bike b = new Bike(200);
}
}
o/p:
brand: BMW
cost: 200