0% found this document useful (0 votes)
3 views14 pages

CoreJava Day 5 ( Class Object Inheritance)

The document provides an overview of core Java concepts, focusing on Object-Oriented Programming (OOP) principles such as classes, objects, inheritance, method overriding, and type casting. It explains the definitions and relationships between classes and objects, various types of inheritance, and the rules for method overriding. Additionally, it covers primitive and class type casting, detailing the processes of widening and narrowing conversions, as well as upcasting and downcasting in Java.

Uploaded by

renukarukare
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views14 pages

CoreJava Day 5 ( Class Object Inheritance)

The document provides an overview of core Java concepts, focusing on Object-Oriented Programming (OOP) principles such as classes, objects, inheritance, method overriding, and type casting. It explains the definitions and relationships between classes and objects, various types of inheritance, and the rules for method overriding. Additionally, it covers primitive and class type casting, detailing the processes of widening and narrowing conversions, as well as upcasting and downcasting in Java.

Uploaded by

renukarukare
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Core Java

- HemanthKumar C

Day 5
Java Oops Concepts
Class and Object
Class is a blueprint or a template to create object .
Object is real time entity which has its own state and behavior

State : state defines the non – static variables , what data it can hold.

Behaviour : behavior defines non – static methods and the way it can behave.
➢ Whenever we create an object we get both state and behavior.
➢ The object address will be stored in reference variable.
➢ Multiple reference variable can hold multiple object address.
Ref1  address  object Ref2  address  object
➢ Any changes made to the object through a reference variable, it will not affect other reference variables.

➢ Multiple reference variable can point ( hold ) single object address.


➢ Any changes made through a reference variable, it will affect other reference variables.

Ref1  address  object Ref2 


Objects Objects
Class Class
INHERITANCE
Inheriting the property from one class to another class is called as
Inheritance.
Ex : single level inheritance
In Inheritance , we have 5 types :
Class Demo {
1. single level Inheritance
2. multi - level Inheritance Int a=10;

3. hierarchical Inheritance }

4. multiple Inheritance Class Sample extends Demo {

5. hybrid Inheritance Void disp ( ) {


System.out.println ( “ hello “ ) ;
1. SINGLE LEVEL INHERITANCE
}}
A subclass inheriting the properties from only super class is called as single
level inheritance. Class Mainclass {
Public static void main ( String [ ] args ) {
DEMO © ➔ Parent class Sample s1= new Sample ( );
➔ Super class
Int a S1.disp( );
➔ base class
System.out.println ( s1.a ) ;

Sample © ➔ child class


➔ Sub class
Void : disp ( )
➔ Derived class
3. HIERARCHICAL INHERITANCE
A multiple sub class inheriting the properties from
only one super class or common super classis called as
hierarchical Inheritance.

Tester ©

Int : x

Sample © Demo ©

Void add ( ) Void add ( )


5. HYBRID INHERITANCE
It is combination of single level , multi – level and hierarchical inheritance.
When to go for inheritance ?
➢ We go for inheritance for code reusability .

Tester 4 ©

Int : x

Demo 4 ©
Sample 6 ©
Void : add ( )
Int : x

Sample 4 ©

Void : disp ( )
METHOD OVERRIDING
Ex: class WhatsApp _v1 {
Developing a method in the sub class with the same
name and signature as in the superclass but with different
implementation in the sub class is called as method Void status ( ) {
overriding.
System.out.println ( “status with text” ) ;
Rules : }}
1.The method name and signature should be same in the
sub class as in the super class. class WhatsApp _ v2 extends WhatsApp _ v1 { Void
2.There should be IS A RELATIONSHIP ( inheritance ). status ( ) {
3.The method be non – static. System.out.println ( “status with text , images ,videos ” ) ;
}}
class Mainclass {
Public static void main ( String [ ] args ) {
WhatsApp w2 = new WhatsApp _ v2 ( ) ; W2 .
status ( ) ;
}}
SUPER KEYWORD
Super keyword is used in the case of method overriding , along with the sub class implementation , if we need super
class implementation and sub class implementaion thus we should go for super . method name
Note :
➢ We go for inheritance for code reusability .
➢ We go for method overriding whenever we want to provide new implementation for the old feature .
➢ We go for method overloading whenever we want to perform common task
and operation .

Note Important : In the case of Method


Overriding Superclass Properties is hidden in the
subclass. This is called as Method Hiding.
TYPE CASTING ( 1 core *** )
Converting from one type to another type is called Type casting.
1. Primitive type casting
• Narrowing → explicitly
• Widening → → Implicitly , explicitly

2. Class type casting / Derived type casting

• Up casting Implicitly , explicitly


• Down casting explicitly
1. Primitive type casting
Converting from one primitive data type to another primitive data type
is called Primitive Type casting.
Widening Implicitly , explicitly
Converting from smaller primitive data type to any of its bigger
primitive data type is called Widening Type casting.
Ex: Implicitly Explicitly
1. double x = 20 ; 1. double x = (double) 20 ;
s.o.p (x) ; → o/p – 20.0d s.o.p (x) ; → o/p – 20.0d
2. double y = 36.6f ; 2. double y = (double) 36.6f ;
s.o.p (y) ; → o/p – 36.6d s.o.p (y) ; → o/p – 36.6d
3. float z= 40 ; 3. float z = (float) 40 ;
s.o.p (z) ; → o/p – 40.0f s.o.p (z) ; → o/p – 40.0f
Narrowing → explicitly
Converting from bigger primitive data type to any of its smaller primitive data type is called Narrowing Type casting.
Explicitly

1. int x = (int) 59.9d; s.o.p(x); → o/p – 59 2. Short y = (short) 36.61; s.o.p(y); → o/p – 36
3. byte z = (byte) 99.9f; s.o.p(z); → o/p – 99 4. Long t = (long) 60.36; s.o.p(y); → o/p – 60

2. Class type casting / Derived type casting / Object type casting


Converting from one class object to another class type is called Derived / class type casting.
Up casting → Implicitly , explicitly
Converting from subclass object to superclass type is called as up casting.
➢ up casting can be done both Implicitly and explicitly.
Down casting → explicitly
Converting from superclass object to subclass type is called as Down casting.
➢ Down casting should be done always explicitly.
➢ Without performing up casting we cannot perform down casting.
➢ Direct down casting is not possible.
➢ To perform upcasting or down casting there should be a Is a relationship.

Note :
Sample s1 = new Sample ( ) ; // homogenous type of object creation. Sample s1 = new Demo ( ) ; // heterogenous type of object creation.
Int a Super class object
Super class
type
( up casting ) ( Down casting )
Sample ©

Void : disp ( )
Sub class type
Sub class object

Ex : Class Demo {
Int a=10;
}
Class Sample extends Demo {

Void disp ( ) {

System.out.println(“hey its disp..”);


}}
Class Mainclass {
Public static void main (String [ ] args) {
Demo D1 = new Sample ( ) ;
System.out.println(D1.a));
Sample S1 = ( Sample ) D1 ;
System.out.println(S1.a)); S1.disp ( ) ;
}}
Thank You

You might also like